import lodash, { forEach } from 'lodash'
import { Graph } from "../graph/graph"
import localforage from 'localforage'
import type { ISlot } from '../graph/slot/instance'
import type { INode } from '../graph/node/instance'
import type { IDeserializable } from '../serialize/IDeserializable'
import { Deserializer } from '../serialize/Deserializer'
import { Dzer } from '../serialize'
import type { VarInit } from '../data/var_init'

export interface IIdentified {
  id: string
}

/**
 * 项目需要存储的内容，而非项目本身的概念
 */
export class ProjectStorage implements IDeserializable {

  className: string = 'ProjectStorage'

  static {
  }

  /** 全局变量 */
  globals: VarInit[] = []

  /** 函数实现图像 */
  graphs: Graph[] = []

  /** 节点对象 */
  nodes: INode[] = []

  /** 插槽对象 */
  slots: ISlot[] = []

  shake() {
    const usedNodes = lodash.flatten(this.graphs.map(x => x.nodesId))
    for (let i=0;i<this.nodes.length;i++) {
      if (!usedNodes.includes(this.nodes[i].id)) {
        (this.nodes[i] as any) = undefined
      }
    }
    this.nodes = this.nodes.filter(x => !!x)

    const usedSlots = lodash.flatten(this.nodes.map(x => x.slotsId))
    for (let i=0;i<this.slots.length;i++) {
      if (!usedSlots.includes(this.slots[i].id)) {
        (this.slots[i] as any) = undefined
      }
    }
    this.slots = this.slots.filter(x => !!x)
  }
}


/**
 * 项目 - 保存完整的解决方案
 */
export class Project implements IDeserializable {

  className: string = 'Project'

  /** 项目存储的复杂类型数据 */
  storage = new ProjectStorage()

  /** 项目名 */
  public name = ''

  // modules
  public modulesEnabled: string[] = ['builtin']
}
